package com.boot.security.server.service.impl.mffl.LwyerPersonalService;

import com.boot.security.server.dao.mffl.MfflLwyerPersonalServiceDao;
import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.SysUser;
import com.boot.security.server.model.mffl.MfflLwyerPersonalService;
import com.boot.security.server.model.mffl.MfflService;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.utils.UserUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boot.security.server.dao.mffl.MfflServiceDao;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类型
 */
@Service
public class LwyerPersonalService {

    @Autowired
    private MfflLwyerPersonalServiceDao mfflLwyerPersonalServiceDao;


    //服务表
    @Autowired
    MfflServiceDao MfflServiceDao;

    //用户表
    @Autowired
    MfflUserDao mffluserdao;

    /**
     * 获取律师可以设置的收费项目
     * @return
     */
    public ReturnBean<MfflService> getServiceList(){
        //获取当前用户
        SysUser sysUser = UserUtil.getLoginUser();
        MfflUser mffluser = mffluserdao.getByUserId(sysUser.getId());

        if (mffluser.getIdentity() != 1) {
            return new ReturnBean("201", "当前用户不是律师");
        }

        List<MfflService> lawyerServiceList = MfflServiceDao.getLawyerServiceList();
        ReturnBean ReturnBean = new ReturnBean<>();
        if (lawyerServiceList.size() == 0){
            ReturnBean.setReturnBean("133");
            return ReturnBean;
        }

        ReturnBean.setDatas(lawyerServiceList);
        return ReturnBean;
    }


    /**
     * 按照id查询律师的收费情况
     * @param id
     * @return
     */
    public ReturnBean<MfflService> getServiceinfo(Integer id){
        //获取当前用户
        SysUser sysUser = UserUtil.getLoginUser();
        MfflUser mffluser = mffluserdao.getByUserId(sysUser.getId());

        if (mffluser.getIdentity() != 1) {
            return new ReturnBean("201", "当前用户不是律师");
        }

        //要设置服务的数据
        MfflService Service = MfflServiceDao.getById(id.longValue());
        if(Service == null){
            return new ReturnBean("201","当前要设置的服务不存在");
        }
        if(Service.getIslawyer() != 1){
            return new ReturnBean("201","要设置的收费产品不是律师个人产品");
        }

        //个人收费情况
        MfflLwyerPersonalService lawyerServiceMoney = getLawyerServiceMoney(sysUser.getId().intValue(), Service.getId().intValue());
        Map<String,String> map = new HashMap<>();

        if (lawyerServiceMoney == null){
            //没设置过
            map.put("name",Service.getName());
            map.put("money","");
            map.put("computingunit",Service.getComputingUnit());
        }else{
            map.put("name",Service.getName());
            map.put("money",lawyerServiceMoney.getMoney().toString());
            map.put("computingunit",Service.getComputingUnit());
        }
        ReturnBean ReturnBean = new ReturnBean<>();
        ReturnBean.setDatas(map);
        return ReturnBean;
    }



    /**
     * 律师设置收费金额
     * @param id
     * @param money
     * @return
     */
    public ReturnBean<MfflService> putServiceMoeny(Integer id,Double money){
        //获取当前用户
        SysUser sysUser = UserUtil.getLoginUser();
        MfflUser mffluser = mffluserdao.getByUserId(sysUser.getId());

        if (mffluser.getIdentity() != 1) {
            return new ReturnBean("201", "当前用户不是律师");
        }
        //要设置服务的数据
        MfflService Service = MfflServiceDao.getById(id.longValue());
        if(Service == null){
            return new ReturnBean("201","当前要设置的服务不存在");
        }
        if(Service.getIslawyer() != 1){
            return new ReturnBean("201","要设置的收费产品不是律师个人产品");
        }

        if(money<Service.getMinimumMoney()){
            return new ReturnBean("201","设置的最小金额不应小于"+(int)Service.getMinimumMoney());
        }

        if(money>Service.getMaximumMoney()){
            return new ReturnBean("201","设置的最大金额不应大于"+(int)Service.getMaximumMoney());
        }

        //个人收费情况
        MfflLwyerPersonalService lawyerServiceMoney = getLawyerServiceMoney(sysUser.getId().intValue(), Service.getId().intValue());
        if(lawyerServiceMoney == null){
            //新增
            MfflLwyerPersonalService mlps = new MfflLwyerPersonalService();
            mlps.setServerId(Service.getId().intValue());
            mlps.setMoney(money);
            mlps.setFreeApplyTime(Service.getFreeapplytime().toString());
            Date date = new Date();
            mlps.setCreateTime(date);
            mlps.setUpdateTime(date);
            mlps.setFreeApplyTime(Service.getFreeapplytime().toString());
            mlps.setDeleteState(1);
            mlps.setDaleteTime(date);
            mlps.setLawyerId(mffluser.getUserid());
            mlps.setOrder(1);
            mlps.setCompany(Integer.parseInt(Service.getDays().split(";")[0]));
            mfflLwyerPersonalServiceDao.save(mlps);
        }else{
            //修改
            MfflLwyerPersonalService mlps = new MfflLwyerPersonalService();
            mlps.setId(lawyerServiceMoney.getId());
            mlps.setMoney(money);
            Date date = new Date();
            mlps.setUpdateTime(date);
            mfflLwyerPersonalServiceDao.update(mlps);
        }
            return new ReturnBean<>();
    }



    //查看律师是否存在这个收费标准
    public MfflLwyerPersonalService getLawyerServiceMoney(Integer lawyerId,Integer serverId){
        MfflLwyerPersonalService mfps = mfflLwyerPersonalServiceDao.getLawyerServiceMoney(lawyerId,serverId);//查询律师收费情况
        return mfps;
    }
}
